﻿using System.Web.Mvc;

using Gonte.Desktop.DataAccess;
using Gonte.Desktop.ExtJs;

namespace Gonte.Desktop.Web.Controllers
{
    public class AppController 
        : Controller
    {
        /// <summary>
        /// Loads the application by its full name
        /// </summary>
        /// <param name="appName">The full name of the application</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SelectApplication(string appName)
        {
            return Content(new Envelope<string>(SessionHelper.SessionVerifier).Process((env) =>
            {
                ApplicationUser user = SessionHelper.ApplicationUser;
                Application app = Application.Load(appName);

                // Create the data service and get the metadata for the application
                SessionHelper.DataService = new MockDataService();
                app.Domain = SessionHelper.DataService.GetMetadata();

                user.SelectedApplication = app;

                return JsonSerializer.Serialize(app);

            }).ToJson());
        }
    }
}
